package com.ysu.shop.controller;

import java.util.Map;
import javax.annotation.Resource;

import com.ysu.shop.service.IRolePermissionService;
import com.ysu.shop.pojo.RolePermission;

import com.ysu.shop.utils.Result;
import com.ysu.shop.utils.InitUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.*;


import lombok.extern.slf4j.Slf4j;

/**
* <p>
    * 用户权限表 前端控制器
    * </p>
*
* @author 耿志龙
* @since 2020-10-01
*/
@RestController
@Slf4j
@RequestMapping("/role-permissions")
public class RolePermissionController {

    @Resource
    private IRolePermissionService iRolePermissionService;

    @GetMapping("/{id}")
    public Result getRightsById(@PathVariable int id) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", id);
        RolePermission role = iRolePermissionService.getOne(wrapper);
        if (role == null) {
            return Result.getFailResult("该角色尚未分配权限");
        }
        return Result.getSuccessResult(role);
    }

    @PostMapping()
    public Result add(@RequestBody RolePermission entity) {
        if (iRolePermissionService.save(entity))
            return Result.getSuccessResult("", "分配权限成功！");
        return Result.getFailResult("", "分配权限失败");
    }

    @PutMapping("/{id}")
    public Result add(@RequestBody RolePermission entity,@PathVariable int id) {
        entity.setId(id);
        if (iRolePermissionService.updateById(entity))
            return Result.getSuccessResult("", "修改分配权限成功！");
        return Result.getFailResult("", "修改分配权限失败");
    }
}
